<!--<template>-->

<!--</template>-->

<!--<script>-->
<!--export default {-->
<!--  name: "CAPSummaryTable"-->
<!--}-->
<!--</script>-->

<!--<style scoped>-->

<!--</style>-->

<template>
  <div class="clearfix">
    <div style="font-size:150%">
      代码扫描个人面板
    </div>
    <div slot="header" class="clearfix">
      项目选择
      <el-select v-model="projectName" placeholder="请选择" @change="changeProject()">
        <el-option
          v-for="item in projectList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span style="color: red">{{ content }}</span>
      <el-tooltip class="item" effect="dark" content="cap数据每天更新，点击fresh获取最新数据" placement="right-end">
        <el-button @click="refreshInfo()" type="primary" style="position: absolute;right: 50px;">刷新</el-button>
      </el-tooltip>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading">
      <el-table-column
        prop="project"
        label="项目"
        width="180">
      </el-table-column>
      <el-table-column
        prop="module"
        label="模块">
      </el-table-column>
      <el-table-column
        prop="branch"
        label="分支">
      </el-table-column>
      <el-table-column
        prop="total_issue"
        sortable
        label="问题个数">
      </el-table-column>
      <el-table-column
        prop="serious_issue"
        sortable
        label="严重问题个数">
      </el-table-column>
      <el-table-column
        prop="current_user_issue"
        sortable
        label="当前用户问题">
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[1, 5,10, 15, 20, 25]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalRow">
    </el-pagination>
  </div>
</template>

<script>
import {api, getRequest} from "@/api/commit";

export default {
  name: "CAPSummaryTable",
  data() {
    return {
      content: "",
      loading: false,
      tableData: [],
      totalRow: 10,
      pageSize: 10,
      currentPage: 1,
      projectName: "CRM",
      startTime: Date.parse(new Date()) / 1000 - 14 * 24 * 60 * 60,
      endTime: Date.parse(new Date()) / 1000,
      projectList: [{
        id: "Membership",
        name: 'MBS'
      }, {
        id: "CRM",
        name: 'CRM'
      }],
    };
  },
  methods: {

    changeProject() {
      this.init_data()
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.init_data();
    },
    //当前页变换
    handleCurrentChange(val) {
      this.currentPage = val;
      this.init_data();
    },

    handleResp(res) {
      res = res.resultReturn
      this.totalRow = res.total
      this.startTime = res.start_time
      this.endTime = Date.parse(new Date()) / 1000
      console.info(this.startTime)

      if (res.status === 0) {
        res = res.data
        this.tableData = res
        for (let i = 0; i < this.tableData.length; i++) {
          let link = "https://cap.epd.i.shopee.io/issue?status=1&status=2&status=3&status=4&ruleLevels=1&ruleTypes=1&" +
            "ruleTypes=2&ruleTypes=3&ruleTypes=4&ruleTypes=5&ruleTypes=6&ruleTypes=7&ruleTypes=8&ruleTypes=9&ruleTypes=" +
            "10&ruleTypes=99" + "&pid=" + this.tableData[i].pid + "&branchName=" + this.tableData[i].branch +
            "&startTime=" + this.startTime + "&endTime=" + this.endTime
          let total_issue_link = link + "&ruleLevels=2&ruleLevels=3&assign"
          this.tableData[i].total_issue =
            <a target="_blank" href={total_issue_link}> {this.tableData[i].total_issue} </a>
          let serious_issue_link = link + "&assign"
          this.tableData[i].serious_issue =
            <a target="_blank" href={serious_issue_link}> {this.tableData[i].serious_issue} </a>
          let current_user_link = total_issue_link + "=" + this.$cookies.get('operator_email') //this.getCookie("operator_email")
          console.info(total_issue_link)
          console.info(serious_issue_link)
          console.info(current_user_link)

          this.tableData[i].current_user_issue =
            <a target="_blank" href={current_user_link}> {this.tableData[i].current_user_issue} </a>
        }
      } else {
        this.tableData = []
        this.content = res.message
      }
      this.loading = false;
    },

    refreshInfo() {
      getRequest(api['get_cap_info'], {
        "project_name": this.projectName,
        "page_size": this.pageSize,
        "page": this.currentPage,
        "cache": false
      }).then((res) => {
        this.handleResp(res)
      }, (error) => {
        console.info(error)
      })
    },
    init_data() {
      this.loading = true;
      getRequest(api['get_cap_info'], {
        "project_name": this.projectName,
        "page_size": this.pageSize,
        "page": this.currentPage
      }).then((res) => {
        this.handleResp(res)
      }, (error) => {
        console.info(error)
      })
    }
  }, created() {
    this.init_data()
  }

};
</script>
